from django.db.models import Q

def remove_leading_zeroes(s):
  """
  Removes leading zeroes from a string.
  """
  if len(s) > 0:
    while s[0] == '0': s = s[1:]
  return s

def student_search_logic(query, queryset, prefix = None):
  """
  Standard search logic for searching students either by student number
  or by their name/surname.
  """    
  if prefix is not None:
    prefix = '%s__' % prefix
  else:
    prefix = ''
  
  try:
    # Check if query is for a student number
    query = int(query)
    queryset = queryset.filter(**{ '%sstudent_number__startswith' % prefix : str(query) })
  except ValueError:
    # Not a student number, assume last name
    queryset = queryset.filter(
      Q(**{ '%ssurname__istartswith' % prefix : query }) |
      Q(**{ '%sname__istartswith' % prefix : query })
    ).distinct()
  
  return queryset

def enrollment_student_search_logic(query, queryset):
  return student_search_logic(query, queryset, prefix = 'student')

def exam_student_search_logic(query, queryset):
  return student_search_logic(query, queryset, prefix = 'student__userprofile')

def subject_search_logic(query, queryset):
  """
  Standard subject search logic.
  """
  return queryset.filter(
    Q(id__istartswith = query) |
    Q(name__istartswith = query) |
    Q(lecturers__userprofile__name__istartswith = query) |
    Q(lecturers__userprofile__surname__istartswith = query)
  ).distinct()

def exam_search_logic(query, queryset):
  """
  Standard exam search logic.
  """
  return queryset.filter(date__icontains = query)

def exam_app_search_logic(query, queryset):
  """
  Standard exam application search logic.
  """
  return queryset.filter(
    Q(id__icontains = query) |
    Q(exam__subject__name__istartswith = query)
  ).distinct()
  
def post_search_logic(query, queryset):
  """
  Standard post search logic.
  """
  try:
    postal_num = int(query)
    return queryset.filter(postal_num__startswith = postal_num)
  except ValueError:
    return queryset.filter(post_office__istartswith = query)
  
def user_profile_search_logic(query, queryset):
  """
  Standard user profile search logic.
  """
  return queryset.filter(
    Q(sifra__istartswith = query) |
    Q(user__username__istartswith = query) |
    Q(user__userprofile__name__istartswith = query) |
    Q(user__userprofile__surname__istartswith = query)
  ).distinct()

def subject_grades_search_logic(query, queryset):
  """
  Used for searching through student's taken exams (see view: student_grades). 
  """
  return queryset.filter(
    Q(exam__subject__id__istartswith = query) |
    Q(exam__subject__name__istartswith = query)
  ).distinct()

#
# (More or less) general search logic functions:
#

def id_or_name_search_logic(query, queryset):
  """
  Filter by id and name.
  """
  return queryset.filter(
    Q(id__istartswith = query) |
    Q(name__istartswith = query)
  ).distinct()
  
def name_search_logic(query, queryset):
  """
  Filter by name.
  """
  return queryset.filter(name__istartswith = query)

